package algorithm;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName ReserveList
 * @Author 刘正星
 * @Date 2021/6/21 下午10:00
 * @Description
 */
public class ReserveList {

    static class ListNode{

        String var;

        ListNode next;

        public ListNode(String var, ListNode next) {
            this.var = var;
            this.next = next;
        }
    }

    public static  void print(ListNode root){
//        if (root !=null){
//            System.out.println(root.var);
//            print(root.next);
//        }
        while (root !=null){
            System.out.println(root.var);
            root = root.next;
        }
    }

    // 1->2->3->null

    // 3->2->1->null

    public static ListNode reserveList(ListNode head){

        //双指针

        ListNode pre = null;
        ListNode cur = head;

        while (cur != null){
            ListNode next = cur.next;

            cur.next = pre;

            pre = cur;

            cur = next;

        }

        return pre;
    }


    //[3,2,4] 6



    public static int[] soultion(int [] arrs,int target){

        HashMap<Integer, Integer> map = new HashMap<>();


        for (int i = 0; i < arrs.length; i++) {

            if (map.containsKey(arrs[i])){
                return new int[] {map.get(arrs[i]),i};
            }
            map.put(target-arrs[i],i);
        }
        return new int [0];
    }

    public static int[] soultion2(int [] arrs,int target){
        for (int i = 0; i < arrs.length; i++) {

            for (int j = i+1; j < arrs.length; j++) {
                if (arrs[i]+arrs[j] == target){
                    return new int[]{i,j};
                }
            }

        }
        return new int [0];
    }

    public static void main(String[] args) {

        ListNode three = new ListNode("3",null);

        ListNode two = new ListNode("2", three);

        ListNode root =new ListNode("1",two);

        print(root);

        root =  reserveList(root);

        print(root);

        int[] soultion = soultion2(new int[]{3, 2, 4}, 6);

        System.out.println(Arrays.toString(soultion));


    }


}
